/**
 * 项目版本获取接口测试用例
 *
 * Created by snowingsea on 2020/02/02.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');

module.exports = describe('/GET project-versions/:id.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion = {};
  const newUserIds = [];
  function url(versionId) {
    if (!versionId) {
      versionId = projectVersion.versionId;
    }
    return `/project-versions/${versionId}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        return project.createRandomVersion();
      })
      .then((res) => {
        projectVersion = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 正常请求
  it('it should GET a project version', function (done) {
    project.creator.login()
      .then(() => {
        chai.request(uri)
          .get(url())
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.projectVersionMessage(res.body);
            res.body.versionId.should.be.eql(projectVersion.versionId);
            res.body.versionName.should.be.eql(projectVersion.versionName);
            res.body.status.should.be.eql(projectVersion.status);
            done();
          });
      })
      .catch(done);
  });

  // 不可获取不存在的项目版本
  it('it should fail to get a project version when the version is not exists', function (done) {
    let projectVersionId;
    project.createRandomVersion()
      .then(({ versionId }) => {
        projectVersionId = versionId;
        return project.removeVersion(versionId);
      })
      .then(() => project.creator.login())
      .then(() => {
        chai.request(uri)
          .get(url(projectVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可获取他人的项目版本
  it('it should fail to get a project version when the project is not yours', function (done) {
    let otherVersionId;
    administrator.createRandomTester()
      .then((newTester) => {
        newUserIds.push(newTester.userId);
        return newTester.createRandomProject();
      })
      .then((project) => project.createRandomVersion())
      .then(({ versionId }) => {
        otherVersionId = versionId;
        return project.creator.login();
      })
      .then(() => {
        chai.request(uri)
          .get(url(otherVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 冻结的项目可获取版本
  it('it should succeed to get project versions when the project is frozen', function (done) {
    let newProjectId;
    let projectVersionId;
    project.creator.createRandomProject()
      .then((project) => {
        newProjectId = project.projectId;
        return project.createRandomVersions(Math.floor(Math.random() * 5) + 1);
      })
      .then(({ versionId }) => {
        projectVersionId = versionId;
        return project.creator.freezeProject(newProjectId);
      })
      .then(() => project.creator.login())
      .then(() => {
        chai.request(uri)
          .get(url(projectVersionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            res.body.versionId.should.be.eql(projectVersion.versionId);
            done();
          });
      })
      .catch(done);
  });
});
